


AddComponentPostInit("builder", function(self)
    local oldGetIngredients = self.GetIngredients
    self.GetIngredients = function(self, recname)   --获取材料
        local ingredients, discounted = oldGetIngredients(self, recname)
        if not self.inst:HasTag("i26") 
        or ingredients == nil
        or not recname or TUNING.I26RZW[recname] == nil
        then
            return ingredients, discounted
        end
        
        local recipe = AllRecipes[recname]
        local ingredients2 = {}
        local discounted2 = false
        for k,v in pairs(recipe.ingredients) do
            if v.amount > 0 then
                local amt = math.max(1, RoundBiasedUp(v.amount * self.ingredientmod * .5))
                local items = self.inst.components.inventory:GetCraftingIngredient(v.type, amt)
                --TheNet:Say(amt)
                ingredients2[v.type] = items
                if amt < v.amount then
                    discounted2 = true
                end
            end
        end
        return ingredients2, discounted2
    
    end

    local oldHasIngredients = self.HasIngredients
    self.HasIngredients = function(self, recipe)   --是否拥有材料[服务器]
        local trec = type(recipe) == "string" and recipe or recipe.name
        
        if not self.inst:HasTag("i26") 
        or not trec 
        or TUNING.I26RZW[trec] == nil 
        then
            --TheNet:Say(trec)
            return oldHasIngredients(self, recipe) 
        end
        if type(recipe) == "string" then 
            recipe = GetValidRecipe(recipe)
        end
        --TheNet:Say(2)
        if recipe ~= nil then
            if self.freebuildmode then
                return true
            end
            for i, v in ipairs(recipe.ingredients) do
                if not self.inst.components.inventory:Has(v.type, math.max(1, RoundBiasedUp(v.amount * self.ingredientmod * .5)), true) then
                    return false
                end
            end
            for i, v in ipairs(recipe.character_ingredients) do
                if not self:HasCharacterIngredient(v) then
                    return false
                end
            end
            for i, v in ipairs(recipe.tech_ingredients) do
                if not self:HasTechIngredient(v) then
                    return false
                end
            end
            return true
        end
    
        return false
    end
end)

AddClassPostConstruct("components/builder_replica", function(self)
    local oldHasIngredients = self.HasIngredients
    self.HasIngredients = function(self, recipe)   --是否拥有材料[本地]
        local trec = type(recipe) == "string" and recipe or recipe.name

        if not self.inst:HasTag("i26") or not trec 
        or not trec or TUNING.I26RZW[trec] == nil then
            return oldHasIngredients(self, recipe) 
        end
        if type(recipe) == "string" then 
            recipe = GetValidRecipe(recipe)
        end
        if self.inst.components.builder ~= nil then
            return self.inst.components.builder:HasIngredients(recipe)
        elseif self.classified ~= nil then
            if type(recipe) == "string" then 
                recipe = GetValidRecipe(recipe)
            end
            if recipe ~= nil then
                if self.classified.isfreebuildmode:value() then
                    return true
                end
                for i, v in ipairs(recipe.ingredients) do
                    if not self.inst.replica.inventory:Has(v.type, math.max(1, RoundBiasedUp(v.amount * self:IngredientMod() * .5)), true) then
                        return false
                    end
                end
                for i, v in ipairs(recipe.character_ingredients) do
                    if not self:HasCharacterIngredient(v) then
                        return false
                    end
                end
                for i, v in ipairs(recipe.tech_ingredients) do
                    if not self:HasTechIngredient(v) then
                        return false
                    end
                end
                return true
            end
        end
    
        return false
    end
end)
